package cn.wolfcode.controller;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Region;
import cn.wolfcode.query.RegionQuery;
import cn.wolfcode.repository.RegionRepository;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IRegionService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("region")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private RegionRepository regionRepository;

    @RequestMapping("list")
    public Object list(Model model,RegionQuery qo){
        Page query = regionService.query(qo);
        model.addAttribute("qo",qo);
        model.addAttribute("page",query);
        return "/region/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        if (!(region.getId() == null)){
            regionService.update(region);
        }else {
            regionService.insert(region);
        }
        return JsonResult.success();
    }

    @RequestMapping("listDest")
    @ResponseBody
    public Object listDest(){
        return destinationService.listAll();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        if (id != null){
            regionService.delete(id);
            return JsonResult.success();
        }
        return JsonResult.defaultError();
    }

    @RequestMapping("changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot,String id){
        regionService.changeIshot(id,hot);
        return JsonResult.success();
    }


    @RequestMapping("getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        return destinationService.getDestByRegionId(rid);
    }
}
